package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.BaseCodeTypeService;
import com.yixun.qfbao.apidto.AddBaseCodeTypeReqDto;
import com.yixun.qfbao.apidto.AddBaseCodeTypeResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.AddBaseCodeTypeReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 标签新增
 * @author guifang.weng
 * @create 2017-11-24 10:04
 **/
@Service
public class AddBaseCodeTypeHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(AddBaseCodeTypeHander.class);

    @Autowired
    private BaseCodeTypeService baseCodeTypeService;

    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BaseResponseDTO<AddBaseCodeTypeResDto> res = new BaseResponseDTO<>();
        AddBaseCodeTypeReqVo reqVo = (AddBaseCodeTypeReqVo)reqDto;
        AddBaseCodeTypeReqDto addBaseCodeTypeReqDto = reqVo.getParams();
        PartnerInfo pi = loginUtils.getUser(addBaseCodeTypeReqDto.getUserId());
        if (pi == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未获取到用户信息");
            return res;
        }
        addBaseCodeTypeReqDto.setMerchantId(pi.getHigherid());
        try {
            AddBaseCodeTypeResDto resDto = baseCodeTypeService.addBaseCodeType(addBaseCodeTypeReqDto);
            res.setResult(resDto.getResult());
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("AddBaseCodeTypeHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("新增标签失败");
        }
        return res;
    }
}
